package com.camemax.rabbitmq.utils;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.Map;

public class ExchangeModules implements Exchanges {
    @Override
    public void declareExchange(String exchangeName,
                                BuiltinExchangeType type,
                                boolean durable,
                                boolean autoDelete,
                                boolean internal,
                                Map<String,Object> args) {
        ConnectionFactory connectionFactory = RabbitMQUtils.createFactory();
        Connection connection = null;
        Channel channel = null;
        try {
            connection = connectionFactory.newConnection();
            channel = connection.createChannel();

            // 调用channel.exchangeDeclare, 通过声明的方式创建exchange。
            channel.exchangeDeclare(
                    exchangeName,
                    type,
                    durable,
                    autoDelete,
                    internal,
                    args
            );

            System.out.println("Exchange [" + exchangeName + "] created successfully!");
        } catch (Exception e) {
            e.printStackTrace();
        }  finally {
            RabbitMQUtils.close(channel, connection);
        }
    }
}
